<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/base_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/character_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/guild_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/error/Error_Log.php');

class error_log_DA extends base_DA {

    const FL_ID = 'id';
    const FL_ERROR_TYPE = 'error_type';
    const FL_CREATION_DATE = 'creation_date';
    const FL_ENTITY_ID = 'entity_id';
    const FL_ENTITY_TYPE = 'entity_type';



    /**
     * (PHP 5)<br/>
     * Get error list
     * @param datetime $min_date_error
     * @return array of Error_Log
     */
    public function get_error_list($min_date_error){
        $result = array();
        $entity_name_field_name = 'name';
        $entity_api_key_field_name = 'api_key';

        $sql_cmd = ' SELECT e.*, '
                  .' CASE WHEN c.'.character_DA::FL_NAME.' IS NOT NULL THEN c.'.character_DA::FL_NAME.' ELSE g.'.guild_DA::FL_NAME.' END AS '.$entity_name_field_name.','
                  .' CASE WHEN c.'.character_DA::FL_API_KEY.' IS NOT NULL THEN c.'.character_DA::FL_API_KEY.' ELSE g.'.guild_DA::FL_API_KEY.' END AS '.$entity_api_key_field_name
                  .' FROM '.$this->get_table_prefix().'_error_log e '
                  .' LEFT JOIN '.$this->get_table_prefix().'_character c ON c.'.character_DA::FL_ID.' = e.'.error_log_DA::FL_ID.' AND e.'.error_log_DA::FL_ENTITY_TYPE.' = '.Error_Log::CHARACTER_ENTITY_TYPE
                  .' LEFT JOIN '.$this->get_table_prefix().'_guild g ON g.'.guild_DA::FL_ID.' = e.'.error_log_DA::FL_ID.' AND e.'.error_log_DA::FL_ENTITY_TYPE.' = '.Error_Log::GUILD_ENTITY_TYPE
                  .' WHERE e.'.error_log_DA::FL_CREATION_DATE.' > '.$this->format_date_to_sql_param($min_date_error);
//        echo '<br>error_log_DA->get_error_list : '.$sql_cmd;
        $log_array = $this->execute_sql_command($sql_cmd, false);

        if ((isset($log_array)) && (count($log_array) > 0) ){
            foreach($log_array as $log){
                $result[] = new Error_Log($log[error_log_DA::FL_ID], $log[error_log_DA::FL_ERROR_TYPE], 
                        $log[error_log_DA::FL_CREATION_DATE], $log[error_log_DA::FL_ENTITY_ID], $log[$entity_name_field_name],
                        $log[$entity_api_key_field_name] ,$log[error_log_DA::FL_ENTITY_TYPE]);
            }
        }

        return (isset($result) ? $result : null);
    }

    /**
     * (PHP 5)<br/>
     * Save the error in DB
     */
    public function insert_error_log(Error_Log $err){
        $sql_cmd = 'INSERT INTO '.$this->get_table_prefix().'_error_log ('.error_log_DA::FL_ERROR_TYPE.', '.error_log_DA::FL_CREATION_DATE.', '
                                  .error_log_DA::FL_ENTITY_ID.', '.error_log_DA::FL_ENTITY_TYPE.') VALUES ('
                                  .$err->get_error_type().', '.$this->format_date_to_sql_param($err->get_creation_date()).', '
                                  .$err->get_entity_id().', '.$err->get_entity_type().')';

//        echo '<br>error_log_DA->save_error_log : '.$sql_cmd;
        $this->execute_sql_command($sql_cmd, true);
        
    }

}

?>
